package com.carboy.launch.widgets;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.carboy.launch.R;
import com.carboy.launch.able.AbstractDialogOperate;

/**
 * 语音操作Dialog
 */
public class SpeechOperateDialog extends Dialog {

	// 标题提示
	private TextView dso_hint_tv;
	// 操作,取消(按钮)
	private Button dso_operate_btn, dso_cancel_btn;
	// 操作LinearLayout
	private LinearLayout dso_operate_linear;
	// 图片提示
	private ImageView dso_hint_igview;
	// Dialog操作抽象类
	private AbstractDialogOperate absDialogOperate;

	public SpeechOperateDialog(Context context) {
		super(context, R.style.Theme_Light_FullScreenDialogAct);
		// 绑定Layout
		this.setContentView(R.layout.dialog_speech_operate);
		// 禁止返回键关闭(模拟可以返回键关闭Dialog，然后立即点击其他页面，看是否会报错----请求数据异步操作，赋值的时候报null(页面已Stop或者销毁了)----测试)
		this.setCancelable(false);

		dso_hint_tv = (TextView) this.findViewById(R.id.dso_hint_tv);
		dso_operate_btn = (Button) this.findViewById(R.id.dso_operate_btn);
		dso_cancel_btn = (Button) this.findViewById(R.id.dso_cancel_btn);
		dso_hint_igview = (ImageView) this.findViewById(R.id.dso_hint_igview);
		dso_operate_linear = (LinearLayout) this
				.findViewById(R.id.dso_operate_linear);
	}

	/**
	 * 设置透明度(针对Dialog)
	 */
	public void setAlpha(float alpha) {
		Window window = SpeechOperateDialog.this.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.alpha = alpha;
		window.setAttributes(lp);
	}

	/**
	 * 设置背景透明度(针对Dialog背景)
	 */
	public void setDimAmount(float dimAmount) {
		Window window = SpeechOperateDialog.this.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.dimAmount = dimAmount;
		window.setAttributes(lp);
		// 背景模糊
		// window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
	}

	/** 设置提示 */
	public void setHintTv(String hint) {
		dso_hint_tv.setText(hint);
	}

	/** 设置提示 */
	public void setHintTv(int hint) {
		dso_hint_tv.setText(hint);
	}

	/** 设置提示 */
	public void setHintTv(int hint, int volume) {
		dso_hint_tv.setText(hint);
		dso_hint_tv.append(":" + volume);
	}

	/** 设置操作按钮提示 */
	private void setOperateTv(int tv) {
		dso_operate_btn.setText(tv);
	}

	/** 设置取消按钮提示 */
	private void setCancelTv(int tv) {
		dso_cancel_btn.setText(tv);
	}

	/**
	 * 设置操作抽象类触发
	 */
	public void setAbsDialogOperate(AbstractDialogOperate adOperate) {
		this.absDialogOperate = adOperate;
		// 设置Dialog返回键监听
		/*
		 * SpeechOperateDialog.this.setOnKeyListener(new OnKeyListener() {
		 * 
		 * @Override public boolean onKey(DialogInterface dialog, int keyCode,
		 * KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK){
		 * absDialogOperate.executeSpeechOperate(true); } return false; } });
		 */
		// 设置点击事件,以及触发操作
		dso_operate_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				judgeDialog(1);
				absDialogOperate.executeSpeechOperate(false);
			}
		});
		dso_cancel_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				absDialogOperate.executeSpeechOperate(true);
			}
		});
	}

	/**
	 * 判断提示框状态
	 */
	public void judgeDialog(int state) {
		switch (state) {
		case 0: // 正常显示提示框
			/*
			 * dso_operate_linear.setVisibility(View.VISIBLE); // 显示按钮
			 * dso_hint_igview
			 * .setBackgroundResource(R.drawable.icon_speech_ready); // 设置语音图片
			 * this.setHintTv(R.string.please_speech);
			 * this.setOperateTv(R.string.start_speech);
			 */
			judgeDialog(1);
			absDialogOperate.executeSpeechOperate(false);
			break;
		case 1: // 点击开始说话
			dso_operate_linear.setVisibility(View.GONE); // 显示按钮
			dso_hint_igview.setBackgroundResource(R.drawable.icon_speech_ready); // 设置语音图片
			this.setHintTv(R.string.please_speech);
			break;
		case 2: // 出现解析失败,无法识别
			dso_operate_linear.setVisibility(View.VISIBLE); // 显示按钮
			dso_hint_igview.setBackgroundResource(R.drawable.icon_speech_error); // 设置出错，失败图片
			this.setHintTv(R.string.distinguish_speech_error);
			this.setOperateTv(R.string.oncemore_please_speech);
			break;
		case 3:// 解析出错，网络异常
			dso_operate_linear.setVisibility(View.VISIBLE); // 显示按钮
			dso_hint_igview.setBackgroundResource(R.drawable.icon_speech_error); // 设置出错，失败图片
			this.setHintTv(R.string.ifly_net_access_timeout);
			this.setOperateTv(R.string.oncemore_please_speech);
			break;
		case 4:// 没听清楚说什么
			dso_operate_linear.setVisibility(View.VISIBLE); // 显示按钮
			dso_hint_igview.setBackgroundResource(R.drawable.icon_speech_error); // 设置出错，失败图片
			this.setHintTv(R.string.ifly_understander_no_data);
			this.setOperateTv(R.string.oncemore_please_speech);
			break;
		case 5:// 识别中
			dso_operate_linear.setVisibility(View.GONE); // 显示按钮
			dso_hint_igview.setBackgroundResource(R.drawable.icon_speech_ready); // 设置语音图片
			this.setHintTv(R.string.distinguishin_speech);
			break;
		}
		this.setCancelTv(R.string.cancel);
	}

	/**
	 * 关闭Dialog
	 */
	public void cancelDialog() {
		if (this.isShowing()) {
			this.cancel();
			this.dismiss();
		}
	}
}
